<script type="text/javascript">
	// 初始化
	jQuery(function() {
		create_grid();
	});

	// 创建grid
	function create_grid() {
		gxc.grid
				.create({
					name : "role_grid",
					url : "$!{paramTool.url_local}/system/user/gridRole.do?user_id=$!{user.id}",
					withCheckbox : false,
					withRownum : true,
					stripeRows : true,
					stripeOverRow : true,
					withPagebar : true,
					pageSize : 10,
					renderTo : "#role_grid",
					fieldSort : "id asc",
					loadDataNow : true,
					columns : [ {
						text : "角色",
						name : "name"
					}, {
						text : "角色描述",
						name : "note"
					}, {
						text : "操作",
						name : "id",
						width : "120px",
						className : "td_operate",
						align : "center",
						render : renderOperate
					} ]
				});
	}

	// grid操作列
	function renderOperate(item) {
		if (item.user_id) {
			var operate = "<a  href='javascript: delete_relate(\"" + item.id
					+ "\")'>取消</a>";
		} else {
			var operate = "<a  href='javascript: create_relate(\"" + item.id
					+ "\")'>关联</a>";
		}
		return operate;
	}

	// 删除关联
	function delete_relate(id) {
		jQuery.ajax({
			type : "post",
			url : "$!{paramTool.url_local}/system/user/deleteRoleRelate.do",
			data : {
				role_id : id,
				user_id : "$!{user.id}"
			},
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					var operate = "<a  href='javascript: create_relate(\"" + id
							+ "\")'>关联</a>";
					jQuery("#" + id + " .td_operate").html(operate)
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		})
	}

	// 创建关联
	function create_relate(id) {
		jQuery.ajax({
			type : "post",
			url : "$!{paramTool.url_local}/system/user/createRoleRelate.do",
			data : {
				role_id : id,
				user_id : "$!{user.id}"
			},
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data, textStatus) {
				if (data.status == "success") {
					var operate = "<a  href='javascript: delete_relate(\"" + id
							+ "\")'>取消</a>";
					jQuery("#" + id + " .td_operate").html(operate)
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function() {
			}
		})
	}

	// 根据关联状态查询
	function changeRelatedStatus() {
		gxc.grid.role_grid.params.related = jQuery("#relatedStatus").val();
		gxc.grid.role_grid.pageCurrent = 1;
		gxc.grid.role_grid.reload();
	}
</script>
